<template>
  <Transition name="toast">
    <div v-if="show" class="c-popup-dialog">
      <div class="panel">
        <header v-if="title" v-text="title" />
        <main v-html="content" />
        <footer class="m-box-center m-justify-aro">
          <van-button class="cancel" size="mini" color="#484C7C" @click="onCancel">Cancel</van-button>
          <van-button class="ok" size="mini" @click="onConfirm">OK</van-button>
        </footer>
      </div>
    </div>
  </Transition>
</template>

<script>
import { noop } from '@/utils'

export default {
  name: 'PopupDialog',
  data() {
    return {
      show: false,
      title: '',
      content: '',
      confirmText: 'ok',
      onClose: noop,
      onSubmit: noop
    }
  },
  created() {
    this.$bus.$on('popupDialog', (content, options = {}) => {
      if (typeof content === 'object') options = content
      else options.content = content
      Object.assign(this.$data, options)

      this.show = true
    })
  },
  methods: {
    onCancel() {
      this.show = false
      this.onClose()
    },
    onConfirm() {
      this.show = false
      this.onSubmit()
    }
  }
}
</script>

<style lang="less" scoped>
.c-popup-dialog {
  display: flex;
  align-items: center;
  position: fixed;
  top: -100px;
  height: calc(~'100% + 200px');
  left: -100px;
  right: -100px;
  padding: 100px;
  background-color: rgba(0, 0, 0, 0.4);
  z-index: 200;

  .panel {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #161a37;
    color: white;
    width: 80%;
    margin: 0 auto;
    border-radius: 10px;
    padding: 0 38px;
    max-height: 70%;
  }

  header {
    flex: none;
    padding: 20px 0;
    font-size: 26px;
  }

  main {
    flex: auto;
    overflow: auto;
    padding: 20px 0;
    font-size: 21px;
  }

  footer {
    flex: none;
    color: @primary;
    text-align: center;
    width: 100%;
    padding: 40px 0;
    .cancel,
    .ok {
      width: 100px;
      padding: 5px 10px;
    }
  }
}
</style>
